
*------------------------------------------------------------------------------*
* EUROPEAN PARLIAMENT
*
* Additional specs: GOV_POS if ideological data is missing
*
* Version:	2016-10-28
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


use tmp_eu, clear
drop p2??

gen _____EUROPEAN_PARLIAMENT = .
save tmp_euparl, replace



* ----------------------
* Standard Positions

gen filterpos = $ideology if p604!=.
replace filterpos = GOV_POS if p604!=. & $ideology == .	// replace with GOV_POS if party pos is missing 


* Mean position, min and max, range
by p601 g105, sort: egen euparl_mean_uw		=   mean(filterpos)
by p601 g105, sort: egen euparl_median_uw	= median(filterpos)
by p601 g105, sort: egen euparl_min			=    min(filterpos)
by p601 g105, sort: egen euparl_max	 		=    max(filterpos)
gen euparl_range = euparl_max - euparl_min
gen euparl_unanimity = euparl_min + (euparl_max - euparl_min)/2

label var euparl_mean_uw 	"European Parliament: Unweighted mean position"
label var euparl_median_uw	"European Parliament: Unweighted median position"
label var euparl_min 		"European Parliament: Minimum position"
label var euparl_max 		"European Parliament: Maximum position"
label var euparl_unanimity	"European Parliament: Position under unanimity rule"
label var euparl_range 		"European Parliament: Range inbetween European Parliament"
save, replace


* Mean and median position weighted by seats
collapse (median) euparl_median = filterpos (mean) euparl_mean = filterpos [fweight = p604], by(p601 g105)
save zs3, replace
use tmp_euparl, clear
merge p601 g105 using zs3, sort uniqusing
drop _merge
sort g101 g105 p101

label var euparl_mean 		"European Parliament: Mean position weighted by seats"
label var euparl_median		"European Parliament: Median position weighted by seats"



* ----------------------
* Model Choice

gen EP_POS = .
gen european_parliament = ""

lab var EP_POS		 		"EU: Position of the European Parliament"
lab var european_parliament	"Your model choice for the European Parliament"


// "Luxembourg compromise", consultation ( -1987q2)
	if 	"$euparl1" == "mean" {
		replace EP_POS = euparl_mean				if g105<=tq(1987q2)
		replace european_parliament = "$euparl1"	if g105<=tq(1987q2)
	}
	else if "$euparl1" == "mean_uw" {
		replace EP_POS = euparl_mean_uw				if g105<=tq(1987q2)
		replace european_parliament = "$euparl1"	if g105<=tq(1987q2)
	}
	else if "$euparl1" == "median" {
		replace EP_POS = euparl_median				if g105<=tq(1987q2)
		replace european_parliament = "$euparl1"	if g105<=tq(1987q2)
	}
	else if "$euparl1" == "median_uw" {
		replace EP_POS = euparl_median_uw			if g105<=tq(1987q2)
		replace european_parliament = "$euparl1"	if g105<=tq(1987q2)
	}
	else if "$euparl1" == "unanimity" {
		replace EP_POS = euparl_unanimity			if g105<=tq(1987q2)
		replace european_parliament = "$euparl1"	if g105<=tq(1987q2)
	}


// Single European Act, cooperation (1987q3-1993q3)
	if 	"$euparl2" == "mean" {
		replace EP_POS = euparl_mean				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_parliament = "$euparl2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$euparl2" == "mean_uw" {
		replace EP_POS = euparl_mean_uw				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_parliament = "$euparl2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$euparl2" == "median" {
		replace EP_POS = euparl_median				if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_parliament = "$euparl2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$euparl2" == "median_uw" {
		replace EP_POS = euparl_median_uw			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_parliament = "$euparl2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$euparl2" == "unanimity" {
		replace EP_POS = euparl_unanimity	if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace european_parliament = "$euparl2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}


// Maastricht Treaty, co-decision I (1993q4 - 1999q1)
	if 	"$euparl3" == "mean" {
		replace EP_POS = euparl_mean				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_parliament = "$euparl3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$euparl3" == "mean_uw" {
		replace EP_POS = euparl_mean_uw				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_parliament = "$euparl3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$euparl3" == "median" {
		replace EP_POS = euparl_median				if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_parliament = "$euparl3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$euparl3" == "median_uw" {
		replace EP_POS = euparl_median_uw			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_parliament = "$euparl3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$euparl3" == "unanimity" {
		replace EP_POS = euparl_unanimity			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace european_parliament = "$euparl3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}


// Amsterdam Treaty, co-decision II (1999q2-2009q4)
	if 	"$euparl4" == "mean" {
		replace EP_POS = euparl_mean				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_parliament = "$euparl4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$euparl4" == "mean_uw" {
		replace EP_POS = euparl_mean_uw				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_parliament = "$euparl4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$euparl4" == "median" {
		replace EP_POS = euparl_median				if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_parliament = "$euparl4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$euparl4" == "median_uw" {
		replace EP_POS = euparl_median_uw			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_parliament = "$euparl4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$euparl4" == "unanimity" {
		replace EP_POS = euparl_unanimity			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace european_parliament = "$euparl4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}


// Lisbon Treaty, co-decision II with rotating presidency (2010q1- )
	if 	"$euparl5" == "mean" {
		replace EP_POS = euparl_mean				if g105 >=tq(2010q1)
		replace european_parliament = "$euparl5"	if g105 >=tq(2010q1)
	}
	else if "$euparl5" == "mean_uw" {
		replace EP_POS = euparl_mean_uw				if g105 >=tq(2010q1)
		replace european_parliament = "$euparl5"	if g105 >=tq(2010q1)
	}
	else if "$euparl5" == "median" {
		replace EP_POS = euparl_median				if g105 >=tq(2010q1)
		replace european_parliament = "$euparl5"	if g105 >=tq(2010q1)
	}
	else if "$euparl5" == "median_uw" {
		replace EP_POS = euparl_median_uw			if g105 >=tq(2010q1)
		replace european_parliament = "$euparl5"	if g105 >=tq(2010q1)
	}
	else if "$euparl5" == "unanimity" {
		replace EP_POS = euparl_unanimity			if g105 >=tq(2010q1)
		replace european_parliament = "$euparl5"	if g105 >=tq(2010q1)
	}
replace european_parliament = "" if EP_POS == .



* ----------------------
* Save temporary file

keep if filterpos != . // keep only parties with seats in the EP
keep g101-g105 p101 $ideology p601-p610 _____GOVERNMENT_POSITION GOV_POS _____EUROPEAN_PARLIAMENT-european_parliament
drop filterpos
save tmp_euparliament, replace

capture: erase zs3.dta
capture: erase tmp_euparl.dta
